package com.autumn.oauth2.zero.domain.entities;

import com.autumn.common.api.domain.entities.IDefaultEntity;
import com.autumn.common.api.domain.entities.auditing.IFullGmt;
import com.autumn.domain.entities.auditing.AuditingConsts;
import com.autumn.mybatis.mapper.annotation.ColumnOrder;
import com.autumn.mybatis.mapper.annotation.Index;
import com.autumn.security.core.userdetails.DefaultAutumnUserDetails;
import com.autumn.util.BeanUtils;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;

/**
 * 用户抽象
 * 
 * @author 老码农 2018-04-09 21:51:39
 */
public abstract class AbstractUser extends DefaultAutumnUserDetails implements IDefaultEntity, IFullGmt {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7908983549985697812L;

	@ColumnOrder(3)
	@Column(name = "nick_name", length = 64, nullable = false)
	private String nickName;
	@ColumnOrder(4)
	@Column(name = "mobile_phone", length = 20, nullable = false)
	@Index
	private String mobilePhone;
	@ColumnOrder(5)
	@Column(name = "mail_address", length = 255, nullable = false)
	@Index
	private String mailAddress;
	@ColumnOrder(6)
	@Column(name = "head_image_path", length = 255, nullable = false)
	private String headImagePath;
	@ColumnOrder(7)
	@Column(name = "status_message", length = 255, nullable = false)
	private String statusMessage;

	@ColumnOrder(10)
	@Column(name = "is_enabled", nullable = false)
	private boolean isEnabled;
	@ColumnOrder(1000)
	@Column(name = AuditingConsts.FIELD_GMT_CREATE, nullable = false)
	private Date gmtCreate;
	@ColumnOrder(1001)
	@Column(name = AuditingConsts.FIELD_GMT_MODIFIED, nullable = false)
	private Date gmtModified;
	@ColumnOrder(1002)
	@Column(name = AuditingConsts.FIELD_IS_DELETE, nullable = false)
	private boolean isDelete;
	@ColumnOrder(1003)
	@Column(name = AuditingConsts.FIELD_GMT_DELETE)
	private Date gmtDelete;

	@Id
	@ColumnOrder(1)
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Override
	public Long getId() {
		return super.getId();
	}

	@Override
	public void setId(Long id) {
		super.setId(id);
	}

	@ColumnOrder(2)
	@Column(name = "user_name", length = 64, nullable = false)
	@Index
	@Override
	public String getUsername() {
		return super.getUsername();
	}

	/**
	 * 获取昵称
	 * 
	 * @return
	 *
	 */
	public String getNickName() {
		return nickName;
	}

	/**
	 * 设置昵称
	 * 
	 * @param nickName
	 *            昵称
	 *
	 */
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	public String getMobilePhone() {
		return this.mobilePhone;
	}

	public void setMailAddress(String mailAddress) {
		this.mailAddress = mailAddress;
	}

	public String getMailAddress() {
		return this.mailAddress;
	}

	/**
	 * 获取头像路径
	 * 
	 * @return
	 *
	 */
	public String getHeadImagePath() {
		return headImagePath;
	}

	/**
	 * 设置头像路径
	 * 
	 * @param headImagePath
	 *            头像路径
	 *
	 */
	public void setHeadImagePath(String headImagePath) {
		this.headImagePath = headImagePath;
	}

	@ColumnOrder(6)
	@Column(name = "password", length = 255, nullable = false)
	@Override
	public String getPassword() {
		return super.getPassword();
	}

	@ColumnOrder(7)
	@Column(name = "is_account_non_expired", nullable = false)
	@Override
	public boolean isAccountNonExpired() {
		return super.isAccountNonExpired();
	}

	@ColumnOrder(8)
	@Column(name = "is_account_non_locked", nullable = false)
	@Override
	public boolean isAccountNonLocked() {
		return super.isAccountNonLocked();
	}

	@ColumnOrder(9)
	@Column(name = "is_credentials_non_expired", nullable = false)
	@Override
	public boolean isCredentialsNonExpired() {
		return super.isCredentialsNonExpired();
	}

	@Override
	public boolean isEnabled() {
		return this.isEnabled;
	}

	@Override
	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	public String getStatusMessage() {
		return statusMessage;
	}

	public void setStatusMessage(String statusMessage) {
		this.statusMessage = statusMessage;
	}

	@Override
	public Date getGmtCreate() {
		if (gmtCreate == null) {
			return new Date();
		}
		return this.gmtCreate;
	}

	@Override
	public void setGmtCreate(Date gmtCreate) {
		this.gmtCreate = gmtCreate;
	}

	@Override
	public Date getGmtModified() {
		return this.gmtModified;
	}

	@Override
	public void setGmtModified(Date gmtModified) {
		this.gmtModified = gmtModified;
	}

	@Override
	public boolean getIsDelete() {
		return this.isDelete;
	}

	@Override
	public void setIsDelete(boolean isDelete) {
		this.isDelete = isDelete;
	}

	@Override
	public Date getGmtDelete() {
		return this.gmtDelete;
	}

	@Override
	public void setGmtDelete(Date gmtDelete) {
		this.gmtDelete = gmtDelete;
	}

	@Override
	public void softDelete() {
		this.setIsDelete(true);
		this.setGmtDelete(new Date());
	}

	/**
	 * 将 null 值转为默认
	 * 
	 *
	 */
	public void forNullToDefault() {
		BeanUtils.setDbEntityforNullToDefault(this);
	}

}
